package com.liuche.pictureserver.controller.picture;

import com.liuche.pictureserver.aop.annotation.AuthCheck;
import com.liuche.pictureserver.aop.annotation.Login;
import com.liuche.pictureserver.aop.annotation.Mask;
import com.liuche.pictureserver.common.resp.BaseResponse;
import com.liuche.pictureserver.common.resp.PageData;
import com.liuche.pictureserver.common.resp.ResUtils;
import com.liuche.pictureserver.pojo.dto.PictureSaveDTO;
import com.liuche.pictureserver.pojo.req.picture.PicturePageReqVO;
import com.liuche.pictureserver.pojo.req.picture.PictureReviewReqVO;
import com.liuche.pictureserver.pojo.req.picture.PictureSaveReqVO;
import com.liuche.pictureserver.pojo.req.picture.PictureSaveUrlReqVO;
import com.liuche.pictureserver.pojo.resp.picture.PictureRespVO;
import com.liuche.pictureserver.pojo.resp.picture.PictureSaveReviewRespVO;
import com.liuche.pictureserver.pojo.resp.picture.PictureSimpleInfoVO;
import com.liuche.pictureserver.pojo.resp.picture.PictureTagCategoryVO;
import com.liuche.pictureserver.service.IPictureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 图片模块
 *
 * @author liuche
 */
@Tag(name = "picture")
@RestController
@RequestMapping("/v1/picture")
public class PictureController {
    @Resource
    private IPictureService pictureService;

    /**
     * 管理员图片上传
     * 1.校验图片信息、判断大小
     * 2.得到图片尺寸等信息
     * 3.保存图片信息
     *
     * @param file 图片信息
     * @return 结果
     */
    @Login
    @Operation(summary = "图片上传Minio")
    @PostMapping("/upload")
    public BaseResponse<PictureSimpleInfoVO> upload(@RequestPart("file") MultipartFile file) {
        PictureSaveDTO dto = new PictureSaveDTO();
        dto.setFile(file);
        return ResUtils.success(pictureService.upload(dto));
    }

    /**
     * 图片上传/更新图片信息
     *
     * @param req 请求信息
     * @return 结果
     */
    @Login
    @Operation(summary = "图片上传")
    @PostMapping("/save")
    public BaseResponse<PictureSaveReviewRespVO> savePicture(@RequestBody @Valid PictureSaveReqVO req) {
        return ResUtils.success(pictureService.save(req));
    }

    /**
     * 删除图片信息
     *
     * @return 结果
     */
    @Login
    @AuthCheck(mustRole = "admin")
    @Operation(summary = "删除图片")
    @DeleteMapping("/del/{id}")
    public BaseResponse<Boolean> delPicture(@PathVariable Long id) {
        return ResUtils.success(pictureService.removeById(id));
    }

    /**
     * 分页查询图片信息
     *
     * @param req 查询信息
     * @return 结果
     */
    @Operation(summary = "分页查询图片信息")
    @PostMapping("/page")
    public BaseResponse<PageData<PictureRespVO>> page(@RequestBody PicturePageReqVO req) {
        return ResUtils.success(pictureService.pageQuery(req));
    }

    /**
     * 根据id获取图片信息
     *
     * @param id 图片id
     * @return 结果
     */
    @Mask
    @Operation(summary = "根据id获取图片详情")
    @GetMapping("/detail/{id}")
    public BaseResponse<PictureRespVO> detail(@PathVariable Long id) {
        return ResUtils.success(pictureService.queryById(id));
    }


    /**
     * 获取标签分类集合
     *
     * @return 结果
     */
    @Operation(summary = "获取标签分类集合")
    @GetMapping("/tagCategory")
    public BaseResponse<PictureTagCategoryVO> listPictureTagCategory() {
        PictureTagCategoryVO pictureTagCategory = new PictureTagCategoryVO();
        List<String> tagList = Arrays.asList("热门", "搞笑", "生活", "高清", "艺术", "校园", "背景", "简历", "创意");
        List<String> categoryList = Arrays.asList("模板", "电商", "表情包", "素材", "海报");
        pictureTagCategory.setTagList(tagList);
        pictureTagCategory.setCategoryList(categoryList);
        return ResUtils.success(pictureTagCategory);
    }

    /**
     * 根据url上传图片
     *
     * @param req url信息
     * @return 图片信息
     */
    @Login
    @Operation(summary = "根据url上传图片")
    @PostMapping("save-url")
    public BaseResponse<PictureSimpleInfoVO> saveUrl(@RequestBody @Valid PictureSaveUrlReqVO req) {
        return ResUtils.success(pictureService.saveByUrl(req.getUrl()));
    }

    /**
     * 审核图片信息
     *
     * @param req 请求信息
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @Operation(summary = "审核图片")
    @PostMapping("review")
    public BaseResponse<Boolean> review(@RequestBody @Valid PictureReviewReqVO req) {
        return ResUtils.success(pictureService.review(req));
    }
}
